unit configbutn;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, ExtCtrls, Graphics, LResources;

type

  { TConfigButton }

  TConfigButton = class(TPanel)
  private
    FButtonLabel: string;
    FButtonVisible: Boolean;
    fCol: integer;
    FItemID: integer;
    FOnSelect: TNotifyEvent;
    FOnVisibilityChanged: TNotifyEvent;
    fRow: Integer;
    FSelected: Boolean;
    VisibleIcon: TBitmap;
    InVisibleIcon: TBitmap;
    VisibilityIcon: TImage;
    procedure SetButtonLabel ( AValue: string ) ;
    procedure SetButtonVisible ( AValue: Boolean ) ;
    procedure SetSelected ( AValue: Boolean ) ;
    procedure ToggleVisibility (Sender: TObject);
  public
    procedure DoOnResize; override;
    procedure Click; override;
    procedure Paint; override;
    constructor Create ( TheOwner: TComponent ) ; override;
    destructor Destroy; override;
    property OnVisibilityChanged: TNotifyEvent read FOnVisibilityChanged write FOnVisibilityChanged;
    property OnSelect: TNotifyEvent read FOnSelect write FOnSelect;
    property ButtonVisible: Boolean read FButtonVisible write SetButtonVisible;
    property Selected: Boolean read FSelected write SetSelected;
    property ItemID: integer read FItemID write FItemID;
    property ButtonLabel: string read FButtonLabel write SetButtonLabel;
    property Row: Integer read fRow write fRow ;
    property Col: integer read fCol write fCol;
  end;

implementation

{ TConfigButton }


function GrowRect(const r: TRect; GrowValue: Integer): TRect;
begin
  Result.Left := R.Left - GrowValue;
  Result.Top := R.Top - GrowValue;
  Result.Right := R.Right + GrowValue;
  Result.Bottom := R.Bottom + GrowValue;
end;



procedure TConfigButton.ToggleVisibility ( Sender: TObject ) ;
begin
  if not FSelected then SetSelected(true);
  SetButtonVisible( not FButtonVisible );
end;

procedure TConfigButton.SetButtonVisible ( AValue: Boolean ) ;
begin
  if FButtonVisible = AValue then Exit;
  FButtonVisible := AValue;
  if FButtonVisible
     then VisibilityIcon.Picture.Bitmap := VisibleIcon
     else VisibilityIcon.Picture.Bitmap := InVisibleIcon;
  if Assigned(FOnVisibilityChanged)
     then FOnVisibilityChanged(Self);
end;

procedure TConfigButton.SetButtonLabel ( AValue: string ) ;
begin
  if FButtonLabel = AValue then Exit;
  FButtonLabel := AValue;
  Refresh;
end;

procedure TConfigButton.SetSelected ( AValue: Boolean ) ;
var
  x: Integer;
begin
  if FSelected = AValue then Exit;
  FSelected := AValue;
  Refresh;
  if FSelected then
    if Assigned(FOnSelect)
       then FOnSelect(self);
end;

procedure TConfigButton.DoOnResize;
begin
  inherited DoOnResize;
  if Assigned( VisibilityIcon)
        then VisibilityIcon.Left := Width-40;
end;

procedure TConfigButton.Click;
begin
  SetSelected(true);
  inherited Click;
end;

procedure TConfigButton.Paint;
var
  styl: TTextStyle;
  R: TRect;
begin
  //inherited Paint;
  Canvas.Pen.Color := clRed;
  Canvas.Pen.Width := 2;
  Canvas.Brush.Color := clBtnFace;
  R := ClientRect;
  R := GrowRect(R, -1);
  if FSelected
     //then Canvas.DrawFocusRect(R);
     then Canvas.Rectangle(R);
  Canvas.Pen.Style := psSolid;
  R := GrowRect(R, -4);
  Canvas.Frame3D(R, cl3DHiLight, cl3DShadow, 2);
  styl.Alignment := taCenter;
  styl.Layout := tlCenter;
  styl.Wordbreak := true;
  styl.SingleLine := false;
  styl.Opaque := false;
  Canvas.TextStyle := styl;
  R := ClientRect;
  R := GrowRect(R, -20);
  Canvas.TextRect(R,0,0,FButtonLabel);
end;

constructor TConfigButton.Create ( TheOwner: TComponent ) ;
begin
  inherited Create ( TheOwner ) ;
  VisibleIcon := TBitmap.Create;
  VisibleIcon.LoadFromLazarusResource('visible-icon');
  InVisibleIcon := TBitmap.Create;
  InVisibleIcon.LoadFromLazarusResource('invisible-icon');
  VisibilityIcon := TImage.Create(self);
  VisibilityIcon.Parent := Self;
  VisibilityIcon.Width := 24;
  VisibilityIcon.Height := 24;
  VisibilityIcon.Picture.Bitmap := VisibleIcon;
  VisibilityIcon.Left := Width-40;
  VisibilityIcon.Top := 10;
  VisibilityIcon.OnClick := @ToggleVisibility;
  FButtonVisible := true;
  FItemID := 0;
end;

destructor TConfigButton.Destroy;
begin
  VisibilityIcon.Free;
  VisibleIcon.Free;
  InVisibleIcon.Free;
  inherited Destroy;
end;


end.


